package com.latsen.pawfit.ext

import android.graphics.BitmapFactory
import android.support.annotation.*
import android.support.v4.content.ContextCompat
import android.support.v4.content.res.ResourcesCompat
import com.hjsmallfly.syllabus.App

private val ctx
    get() = App.getContext()

private val res
    get() = ctx.resources

fun getDimenPixelSizeById(@DimenRes resId: Int) = res.getDimensionPixelSize(resId)

fun getStringArrayById(@ArrayRes resId: Int) = res.getStringArray(resId)
fun getIntArrayById(@ArrayRes resId: Int) = res.getIntArray(resId)
fun getTextArray(@ArrayRes resId: Int) = res.getTextArray(resId)

fun getStringById(@StringRes resId: Int) = res.getString(resId)

fun getDrawableById(@DrawableRes resId: Int) = ContextCompat.getDrawable(ctx, resId)

fun getDrawableBitmapById(@DrawableRes resId: Int) = BitmapFactory.decodeResource(res, resId)

fun getColorById(@ColorRes resId: Int) = ContextCompat.getColor(ctx, resId)

fun dp2px(dp: Float): Int {
    val scale = ctx.resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}

fun px2dp(px: Int): Float {
    val scale = ctx.resources.displayMetrics.density
    return px / scale + 0.5f
}

fun sp2px(sp: Int): Int {
//    val scale = res.displayMetrics.scaledDensity
//    return (sp * scale + 0.5f).toInt()
    return dp2px(sp.toFloat())
}

//fun toUriString(@DrawableRes resId: Int): String = ResourcesCompat.toUriString(resId)

val statusBarHeight: Int
    get() {
        var statusBarHeight = 0
        //获取status_bar_height资源的ID
        val resourceId = res.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = res.getDimensionPixelSize(resourceId)
        }
        return statusBarHeight
    }

//fun newDivLineItemDecoration(@DrawableRes drawable: Int, height: Int = 1): RecyclerView.ItemDecoration {
//    val decLineDrawable = getDrawableById(drawable)
//    val itemDecoration = DividerItemDecoration(
//            App.getContext(), DividerItemDecoration.VERTICAL_LIST, decLineDrawable)
//    itemDecoration.setHeight(height)
//    return itemDecoration
//}